package com.venutech.gongan.api;

import com.baomidou.mybatisplus.plugins.Page;
import com.venutech.gongan.common.Constants;
import com.venutech.gongan.common.Messages;
import com.venutech.gongan.entity.Jkxt;
import com.venutech.gongan.entity.RzLog;
import com.venutech.gongan.entity.Yjyd;
import com.venutech.gongan.model.ResultFormat;
import com.venutech.gongan.service.JkxtService;
import com.venutech.gongan.service.YjydService;
import com.venutech.gongan.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"一机一档控制层接口"})
public class YjydController extends AbstractControllerBase {
    @Resource
    private YjydService yjydService;
    @Resource
    private JkxtService jkxtService;

    @GetMapping("/yjyd_List")
    public Map<String, Object> yjyd_List(@RequestParam Integer page, @RequestParam Integer limit,
                                         @RequestParam(required = false) String begintime,
                                         @RequestParam(required = false) String endtime,
                                         @RequestParam(required = false) String badw,
                                         @RequestParam(required = false) String yj_id) {
        Map<String, Object> result = new HashMap<>();
        //查询监控系统所有数据
        List<Jkxt> jkxtList = jkxtService.getSelectAll();
        if (yj_id==null) {
            Page<Yjyd> p = yjydService.getInfo2(page, limit, begintime, endtime, badw);
            List<Yjyd> yjydList = p.getRecords();
            for (int i = 0; i <jkxtList.size() ; i++) {
                Jkxt jkxt = jkxtList.get(i);
                for (int j = 0; j <yjydList.size() ; j++) {
                    Yjyd yjyd = yjydList.get(j);
                    if (jkxt.getJk_id()==yjyd.getYj_jk_id()){
                        yjyd.setYj_jk_name(jkxt.getJk_name());
                    }
                }
            }
            result.put("code", "0");
            result.put("msg", "成功");
            result.put("count", p.getTotal());
            result.put("data", p.getRecords());
        }else {
            Page<Yjyd> p = yjydService.getInfo(page, limit, begintime, endtime, badw, Integer.parseInt(yj_id));
            List<Yjyd> yjydList = p.getRecords();
            for (int i = 0; i <jkxtList.size() ; i++) {
                Jkxt jkxt = jkxtList.get(i);
                for (int j = 0; j <yjydList.size() ; j++) {
                    Yjyd yjyd = yjydList.get(j);
                    if (jkxt.getJk_id()==yjyd.getYj_jk_id()){
                        yjyd.setYj_jk_name(jkxt.getJk_name());
                    }
                }
            }
            result.put("code", "0");
            result.put("msg", "成功");
            result.put("count", p.getTotal());
            result.put("data", p.getRecords());
        }

        return result;
    }

    @PostMapping(value = "/yjydSave")
    @ApiOperation(value = "一机一档保存页面")
    public ResultFormat yjydSave(Yjyd yjyd){
        RzLog rzLog = new RzLog();

        ResultFormat insert = yjydService.insert(yjyd);
        if (insert.getCode() == 200) {
            rzLog.setRz_user_id(currentUserId().intValue());
            rzLog.setRz_type(Constants.INSERT);
            rzLog.setRz_msg(Messages.YJYD_ADD_SUCCESS);
            rzLog.setRz_state(Constants.LOG_OK);
            rzLogService.insert(rzLog);
            return ResultUtil.success();
        }
            rzLog.setRz_user_id(currentUserId().intValue());
            rzLog.setRz_type(Constants.INSERT);
            rzLog.setRz_msg(Messages.YJYD_ADD_Fail);
            rzLog.setRz_state(Constants.LOG_NO);
            rzLogService.insert(rzLog);

        return ResultUtil.error(100, "添加失败");

    }

    @PostMapping("/yjydDelete")
    public ResultFormat yjydDelete(@RequestParam String yj_id) {
        RzLog rzLog = new RzLog();
        try {
            int i = yjydService.deleteById(Integer.parseInt(yj_id));
            if (i > 0) {
                rzLog.setRz_user_id(currentUserId().intValue());
                rzLog.setRz_type(Constants.DELETE);
                rzLog.setRz_msg(Messages.YJYD_DELETE_SUCCESS);
                rzLog.setRz_state(Constants.LOG_OK);
                rzLogService.insert(rzLog);
                //正常
                return ResultUtil.success();
            }else {
                rzLog.setRz_user_id(currentUserId().intValue());
                rzLog.setRz_type(Constants.DELETE);
                rzLog.setRz_msg(Messages.YJYD_DELETE_Fail);
                rzLog.setRz_state(Constants.LOG_NO);
                rzLogService.insert(rzLog);
                //异常
                return ResultUtil.error(-1, "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(-1, "");
    }

    @ApiOperation("一机一档更新可能用到的 Options")
    @GetMapping("/yjyd/options")
    public Map<String, Object> yjydUpdateView() {
        RzLog rzLog = new RzLog();

        //查询监控系统所有数据
        List<Jkxt> list = jkxtService.getSelectAll();

        Map<String, Object> result = new HashMap<>();
        result.put("jkxtList", list);
        return result;
    }

    @ApiOperation("详情")
    @GetMapping("/yjyd/detail/{id}")
    public ResultFormat<Yjyd> detail(@PathVariable("id") Integer id) {
        return ResultUtil.success(yjydService.getById(id));
    }

    @PostMapping(value = "/yjydUpdate")
    public ResultFormat yjydUpdate(Yjyd yjyd){
        RzLog rzLog = new RzLog();
        int i = yjydService.updateYjyd(yjyd);
        if (i>0) {
            rzLog.setRz_user_id(currentUserId().intValue());
            rzLog.setRz_type(Constants.UPDATE);
            rzLog.setRz_msg(Messages.YJYD_UPDATE_SUCCESS);
            rzLog.setRz_state(Constants.LOG_OK);
            rzLogService.insert(rzLog);
        }else {
            rzLog.setRz_user_id(currentUserId().intValue());
            rzLog.setRz_type(Constants.UPDATE);
            rzLog.setRz_msg(Messages.YJYD_UPDATE_FAIL);
            rzLog.setRz_state(Constants.LOG_NO);
            rzLogService.insert(rzLog);
        }

        return ResultUtil.success();
    }
}
